import { BaseProps } from '@store/store'
import { message, notification } from 'antd'
import * as React from 'react'
import { api } from './api'
import { EventEmitter } from './EventEmitter'
import utils from './utils'

/**
 * 扩展组件/store类以方便调用
 * 集成api, 公用组件
 */
export abstract class ReactComponentExt<P extends BaseProps, S = {}> extends React.PureComponent<P, S> {
  public readonly api = api
  public readonly $message = message
  public readonly $notification = notification
  public props: P
  public state!: S
  public query!: { [key: string]: any }
  constructor(props: P) {
    super(props)
    this.normalizeProps(props)
    this.props = props
    this.init()
  }

  public abstract init(): void

  private normalizeProps(props: P) {
    this.query = utils.camelize(utils.getUrlArgs(location.search)!)!
  }
}

export abstract class StoreExt<P> extends EventEmitter<P> {
  public readonly api = api
  public readonly $message = message
  public readonly $notification = notification
}
